#editProportion.py
#Author: Guanyu Wang
#

import os
import sys

def main():

	path1 = sys.argv[1]

	f = open(path1, "r")
        dic = {}
	for line in f:
		pieces = line.split(' ')
                if pieces[0] == "Stats":
                        year = pieces[-1].strip()[-8:-4]
                if pieces[0] <> "TOTAL" and pieces[0] <> "Stats":
                        dic[year+pieces[0]] = float(pieces[1].replace('\n',''))    

        path = sys.argv[2]
	files = os.listdir(path)
        for folder in files:
                namespace = os.listdir(path+folder)
		for nsfile in namespace:
			f = open(path+folder+"/"+nsfile, "r")
			propList = []
		        edityear = nsfile[-8:-4]
			for line in f:
				parts = line.split(' ')
		                if parts[0] <> "TOTAL" and parts[0] <> "Stats":
		                        prop = float(parts[1].replace('\n',''))/dic[edityear+parts[0]] 
					propList.append(str(prop))
			printable(propList,nsfile)	


def printable(aList,filename):
	print "prop_" + filename[:-8] + "_" + filename[-8:-4] + " <- c(" + ",".join(aList) + ')'

main()
